//表格行
let columns = [[
    {field: 'taskState', title: '任务状态', align:'center', width:100, formatter: formatterState},
    {field: 'taskNum', title: '任务数', align:'center', width:100}
]];

//下拉列表的数据
let comboboxData = [
    {
        "value":0,
        "text":"离线下载任务"
    },
    {
        "value":1,
        "text":"断点上传任务"
    }
]

//数据
let _data;
let _type;

$(function() {
    //初始化类型
    _type = 1;

    //请求数据
    getData();

    //数据表格初始化
    $('#grid').datagrid({
        columns:columns,
        singleSelect:true,
        fitColumns:true
    });

    //初始化下拉列表
    $("#taskType").combobox({
        data: comboboxData,
        valueField:'value',
        textField:'text',
        onSelect: function (record) {
            //改变类型
            _type = record.value + 1;
            //重新请求数据
            getData();
        }
    })
});

//请求数据
function getData() {
    //加载完成后异步请求数据
    $.ajax({
        url: "admin_taskStateInfo",
        data: JSON.stringify({"type":_type}),
        contentType : "application/json",
        type:'post',
        dataType:'json',
        success:function(value){
            //console.log(JSON.stringify(value));
            if(value.success) {
                _data = value.data;
                //显示数据表格
                $("#grid").datagrid("loadData", showDataGrid());
                //显示图表
                showChart();
            } else {
                $.messager.alert('提示',value.data, "info");
            }
        }
    });
}

//显示表格数据
function showDataGrid() {
    let chartData = {};
    let rows = [];
    for(let i = 0; i < _data.length; i++) {
        rows.push({
            "taskState": _data[i].state,
            "taskNum": _data[i].number
        });
    }
    chartData.total = rows.length;
    chartData.rows = rows;
    return chartData;
}

//显示图表
function showChart() {
    var chart = {
        plotBackgroundColor: null,
        plotBorderWidth: null,
        plotShadow: false
    };
    var title = {
        text: (_type == 1 ? '离线下载' : '断点上传') + '任务状态统计图'
    };
    var tooltip = {
        pointFormat: '{series.name}: <b>{point.percentage:.0f}%</b>'
    };
    var plotOptions = {
        pie: {
            allowPointSelect: true,
            cursor: 'pointer',
            dataLabels: {
                enabled: true,
                format: '<b>{point.name}</b>: {point.percentage:.0f} %',
                style: {
                    color: (Highcharts.theme && Highcharts.theme.contrastTextColor) || 'black'
                }
            }
        }
    };

    var series= [{
        type: 'pie',
        name: '占比',
        data: getChartData()
    }];

    var json = {};
    json.chart = chart;
    json.title = title;
    json.tooltip = tooltip;
    json.series = series;
    json.plotOptions = plotOptions;
    $('#container').highcharts(json);
}

//格式化状态值
function formatterState(value, row, index) {
    if(_type == 1) {
        return getDownloadStateValue(value);
    } else {
        return getUploadStateValue(value);
    }
}

//获取下载任务状态值
function getDownloadStateValue(state) {
    switch (state) {
        case 0:
            return "等待中";
        case 1:
            return "正在下载";
        case 2:
            return "下载完成";
        case 3:
            return "取消下载";
            //break;
        case 4:
            return "等待重新连接";
        case 5:
            return "下载失败";
        case 6:
        case 7:
            return "已删除";
    }
}

//获取上传任务状态值
function getUploadStateValue(state) {
    switch (state) {
        case 0:
            return "等待中";
        case 1:
        case 3:
        case 5:
            return "正在上传";
        case 2:
            return "暂停上传";
        case 4:
        case 7:
            return "上传失败";
        case 6:
            return "上传完成"
        case 8:
            return "取消上传";
        case 9:
            return "已删除";
    }
}

function getChartData() {
    let chartData = [];
    for(let i = 0; i < _data.length; i++) {
        let itemData = [];
        itemData.push(formatterState(_data[i].state));
        itemData.push(_data[i].number);
        chartData.push(itemData);
    }
    return chartData;
}